package com.aiyige.page.edituserinfo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.URIEncodeUtil;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.net.URLDecoder;
import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.EditBannerUrlPage)
public class EditBannerUrlPage extends AppCompatActivity {
    public static final String EXTRA_KEY_BANNER_URL = "com.aiyige.extra.EXTRA_KEY_BANNER_URL";
    public static final String EXTRA_KEY_BANNER_IMAGE_URL = "com.aiyige.extra.EXTRA_KEY_BANNER_IMAGE_URL";

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.bannerIv)
    ImageView bannerIv;
    @BindView(R.id.inputUrlEt)
    EditText inputUrlEt;
    @BindView(R.id.inputUrlClearBtn)
    ImageView inputUrlClearBtn;
    @Autowired
    String imageUrl;
    @Autowired
    String bannerUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_edit_banner_url);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.edit);
        Glide.with(this)
                .load(imageUrl)
                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                .into(bannerIv);
        inputUrlEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0) {
                    inputUrlClearBtn.setVisibility(View.VISIBLE);
                } else {
                    inputUrlClearBtn.setVisibility(View.INVISIBLE);
                }
            }
        });
        try {
            inputUrlEt.setText(URLDecoder.decode(bannerUrl, "UTF-8"));
        } catch (Exception e) {
            inputUrlEt.setText("");
        }
        inputUrlEt.setSelection(inputUrlEt.getText().length());
    }

    @OnClick({R.id.titleBackBtn, R.id.saveBtn, R.id.inputUrlClearBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.saveBtn:
                bannerUrl = inputUrlEt.getText().toString().trim();
                if (!TextUtils.isEmpty(bannerUrl)) {
                    try {
                        Uri uri = Uri.parse(bannerUrl);
                        String scheme = uri.getScheme();
                        if (TextUtils.isEmpty(scheme)) {
                            bannerUrl = "http://" + bannerUrl;
                        }
                        if (!Patterns.WEB_URL.matcher(bannerUrl).matches()) {
                            throw new Exception("INVALID URL");
                        }
                        bannerUrl = URIEncodeUtil.encode(bannerUrl);
                    } catch (Exception e) {
                        ToastX.show(R.string.please_input_valid_url);
                        break;
                    }
                }
                Intent intent = new Intent();
                intent.putExtra(EXTRA_KEY_BANNER_URL, bannerUrl);
                intent.putExtra(EXTRA_KEY_BANNER_IMAGE_URL, imageUrl);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.inputUrlClearBtn:
                inputUrlEt.setText("");
                break;
        }
    }
}
